﻿Imports System.Data.OleDb

Public Class hotelDetail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            If Request.Params("ID") = "" Then Response.Redirect("Hotels.aspx")
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("HotelsConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM Comments WHERE Hotel_fkID =@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            oleDbConn.Open()
            Dim dataReader = cmd.ExecuteReader()
            GridView2.DataSource = dataReader
            GridView2.DataBind()

            Dim hotelsViwedSql As String = "INSERT INTO HotelsViewed(Travellor,Hotel_fkID) VALUES(@f1,@f2)"
            Dim cmd2 As OleDbCommand = New OleDbCommand(hotelsViwedSql, oleDbConn)
            cmd2.CommandType = CommandType.Text
            cmd2.Parameters.AddWithValue("@f1", User.Identity.Name)
            cmd2.Parameters.AddWithValue("@f2", Request.Params("ID"))
            cmd2.ExecuteNonQuery()
        End If
    End Sub

    Protected Sub btn_AddComment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_AddComment.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("HotelsConnectionString").ConnectionString)
        Dim SqlString As String = "Insert into Comments(Author,Comment,Title,Hotel_fkID) Values (@f1,@f2,@f3,@f4)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_Comment.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Title.Text)
        cmd.Parameters.AddWithValue("@f4", Request.Params("ID"))
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        ''''Fill GridView2
        Dim fillComments As String = "SELECT * FROM Comments WHERE Hotel_fkID =@f1"
        Dim fillCommentsCmd As OleDbCommand = New OleDbCommand(fillComments, oleDbConn)
        fillCommentsCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim dataReader = fillCommentsCmd.ExecuteReader()
        GridView2.DataSource = dataReader
        GridView2.DataBind()
        tb_Comment.Text = ""
        tb_Title.Text = ""

        'Response.Redirect("commentConfirmation.aspx")
    End Sub

    Protected Sub GridView2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles GridView2.SelectedIndexChanged

    End Sub


    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("HotelsConnectionString").ConnectionString)
        Dim SqlString As String = "Insert into HotelTagJunction(Hotel_fkID,Tag_fkID ) Values (@f1,@f2)"
        oleDbConn.Open()

        For Each cb As ListItem In CheckBoxList1.Items
            If cb.Selected Then
                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next
        If tb_Tags.Text = "" Then Return
        Dim newTags = tb_Tags.Text.Split(",")
        For Each newTag In newTags
            'first add new tags to the Tags table
            Dim newTagSql As String = "INSERT INTO Tags(Tags) Values (@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(newTagSql, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", newTag)
            cmd.ExecuteNonQuery()
            'Then find the id of the new tag
            Dim getLastIdCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID from Tags ORDER BY ID DESC", oleDbConn)
            Dim dr = getLastIdCmd.ExecuteReader()
            dr.Read()
            Dim lastId = dr.GetValue(0)

            'Associate this last id with a hotel

            Dim newCmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            newCmd.CommandType = CommandType.Text
            newCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            newCmd.Parameters.AddWithValue("@f2", lastId)
            newCmd.ExecuteNonQuery()
        Next
        tb_Tags.Text = ""
    End Sub
End Class